package cn.iocoder.yudao.module.design.service.report;

import cn.iocoder.yudao.module.design.dal.dataobject.house.HouseDO;
import cn.iocoder.yudao.module.design.service.diary.DiaryService;
import cn.iocoder.yudao.module.design.service.house.HouseService;
import cn.iocoder.yudao.module.design.service.pic.PicService;
import com.google.common.collect.Lists;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.stream.Collectors;

@Service
public class UserReportServiceImpl implements UserReportService
{
    @Resource
    PicService picService;
    @Resource
    DiaryService diaryService;

    @Resource
    HouseService houseService;
    @Override
    public void generateUserReport(String userId) {
        /**
         * todo
         * 1、获取有效的房屋
         * 2、计算需要的线程数量
         */

        List<HouseDO> houseLst =houseService.getHouseByStatus(1);
        //1000个房屋一个线程
        int threadNum =houseLst.size()/1000>10?10:houseLst.size()/1000+1;
        if(houseLst.size() ==0)
        {
            return;
        }
        ExecutorService exe = Executors.newFixedThreadPool(threadNum);
        List<List<HouseDO>> splitHouseList =splitList(houseLst,1000);
        List<Future<String>> resultList= Lists.newArrayList();
        for (List<HouseDO> houseDOList:splitHouseList) {
            Future<String> future=exe.submit(new Callable<String>() {
                @Override
                public String  call() throws Exception {
                    try{
                        //todo 完成日记数据生成并插入日记表
                        dealData(houseDOList);
                    }
                    catch (Exception e)
                    {
                        return  "error";
                    }
                    return "success";
                }
            });
            resultList.add(future);
        };
        exe.shutdown();

        for(Future<String>fs:resultList)
        {
            try{
                String result=fs.get();
                if(!result.startsWith("sucess"))
                {
                    throw new RuntimeException(result);
                }
            }catch (Exception e)
            {
                throw new RuntimeException(e.getMessage());
            }
        }
        picService.getPicList((Collection<Long>) null);
    }
    private List<List<HouseDO>> splitList(List<HouseDO> lst,int cnt)
    {
        int length=lst.size();
        int num =(length+cnt-1)/cnt;
        List<List<HouseDO>> retLst=new ArrayList<>();
        for (int i = 0; i < num; i++) {
            int fromIndex =i*cnt;
            int toIndex=Math.min((i+1)*cnt,length);
            retLst.add(lst.subList(fromIndex,toIndex));
        }
        return  retLst;
    }
    private void dealData(List<HouseDO>inList)
    {
        Map<Long,HouseDO> houseMap =inList.stream().collect(Collectors.toMap(HouseDO::getId, e->e));
        String ids="";
        for (Long tmp : houseMap.keySet())
        {
            ids+=tmp+",";
        }
        ids=ids.substring(0,ids.length()-1);
        List<Map> lst =picService.getPicListByIds(ids);

        //插入当天的日记
        diaryService.createDiarys(lst);
    }
}
